<template>
  <Dialog ref="dialogs" title="物流信息" width="500">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
      class="demo-ruleForm"
      status-icon
    >
      <el-form-item label="物流公司" prop="expressCompany">
        <el-input
          v-model="ruleForm.expressCompany"
          placeholder="请输入物流公司名称"
        />
      </el-form-item>
      <el-form-item
        style="margin-top: 20px"
        label="物流单号"
        prop="expressNumber"
      >
        <el-input
          v-model="ruleForm.expressNumber"
          placeholder="请输入物流单号"
        />
      </el-form-item>
      <el-divider />
      <div class="right">
        <el-button @click="colsechange">取消</el-button>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >确定</el-button
        >
      </div>
    </el-form>
  </Dialog>
</template>
<script setup>
import Dialog from '@/components/Dialog/Dialog.vue';
import { ref, reactive } from 'vue';
import { userOrderSend } from '@/api/manage/manage.js';
import { ElMessage, ElMessageBox } from 'element-plus';

const emit = defineEmits(['refreshUserList']);

const dialogs = ref();
const ruleFormRef = ref();
const ruleForm = reactive({
  expressCompany: '',
  expressNumber: '',
});
const orderInfo = ref({});

const rules = reactive({
  expressCompany: [
    {
      required: true,
      message: '请输入物流公司',
      trigger: 'blur',
    },
  ],
  expressNumber: [
    {
      required: true,
      message: '请输入物流单号',
      trigger: 'blur',
    },
  ],
});

// 确认发货
function submitForm(formEl) {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      ElMessageBox.confirm('确认发货吗？').then(() => {
        const data = {
          expressCompany: ruleForm.expressCompany,
          expressNumber: ruleForm.expressNumber,
          orderNo: orderInfo.value.orderNo,
        };
        userOrderSend(data).then(() => {
          ElMessage({
            type: 'success',
            message: '已发货',
          });

          emit('refreshUserList');
          colsechange();
        });
      });
    }
  });
}

function colsechange() {
  dialogs.value.closeChange();
}

function openChange(val) {
  orderInfo.value = val;
  dialogs.value.openChange();
}

defineExpose({
  openChange,
});
</script>
